package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.SysUserProject;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.SysProject;
import com.ruoyi.system.service.ISysProjectService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目Controller
 * 
 * @author sux
 * @date 2021-09-03
 */
@Controller
@RequestMapping("/system/project")
public class SysProjectController extends BaseController
{
    private String prefix = "system/project";

    @Autowired
    private ISysProjectService sysProjectService;

    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("project:sysProject:view")
    @GetMapping()
    public String sysProject()
    {
        return prefix + "/sysProject";
    }

    /**
     * 查询项目列表
     */
    @RequiresPermissions("project:sysProject:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysProject sysProject)
    {
        startPage();
        List<SysProject> list = sysProjectService.selectSysProjectList(sysProject);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @RequiresPermissions("project:sysProject:export")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysProject sysProject)
    {
        List<SysProject> list = sysProjectService.selectSysProjectList(sysProject);
        ExcelUtil<SysProject> util = new ExcelUtil<SysProject>(SysProject.class);
        return util.exportExcel(list, "sysProject");
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目
     */
    @RequiresPermissions("project:sysProject:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysProject sysProject)
    {
        return toAjax(sysProjectService.insertSysProject(sysProject));
    }

    /**
     * 修改项目
     */
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        SysProject sysProject = sysProjectService.selectSysProjectById(projectId);
        mmap.put("sysProject", sysProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目
     */
    @RequiresPermissions("project:sysProject:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysProject sysProject)
    {
        return toAjax(sysProjectService.updateSysProject(sysProject));
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("project:sysProject:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysProjectService.deleteSysProjectByIds(ids));
    }

    /**
     * 分配用户
     */
    @RequiresPermissions("project:sysProject:edit")
    @GetMapping("/authUser/{projectId}")
    public String authUser(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        mmap.put("project", sysProjectService.selectSysProjectById(projectId));
        return prefix + "/authUser";
    }

    /**
     * 查询已分配用户项目列表
     */
    @RequiresPermissions("project:sysProject:list")
    @PostMapping("/authUser/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectProjectAllocatedList(user);
        return getDataTable(list);
    }
    /**
     * 查询未分配用户项目列表
     */
    @RequiresPermissions("project:sysProject:list")
    @PostMapping("/authUser/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectProjectUnallocatedList(user);
        return getDataTable(list);
    }

    /**
     * 取消授权
     */
    @Log(title = "项目管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/cancel")
    @ResponseBody
    public AjaxResult cancelAuthUser(SysUserProject project)
    {
        return toAjax(sysProjectService.deleteAuthUser(project));
    }

    /**
     * 批量取消授权
     */
    @Log(title = "项目管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/cancelAll")
    @ResponseBody
    public AjaxResult cancelAuthUserAll(Long projectId, String userIds)
    {
        return toAjax(sysProjectService.deleteAuthUsers(projectId, userIds));
    }
    /**
     * 选择用户
     */
    @GetMapping("/authUser/selectUser/{projectId}")
    public String selectUser(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        mmap.put("role", sysProjectService.selectSysProjectById(projectId));
        return prefix + "/authUser/selectUser";
    }


    /**
     * 批量选择用户授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/selectAll")
    @ResponseBody
    public AjaxResult selectAuthUserAll(Long projectId, String userIds)
    {
        return toAjax(sysProjectService.insertAuthUsers(projectId, userIds));
    }
    /**
     * 用户状态修改
     */
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("project:sysProject:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysProject project)
    {
        return toAjax(sysProjectService.changeStatus(project));
    }
}
